package com.xique.door.bean.validation;


import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.DCard;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.request.card.CardAddRequest;
import com.xique.door.service.IDCardService;
import com.xique.door.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Auther: caogq
 * @Date: 2021/7/7 11:32
 * @Description:
 */
@Component
public class CardValidation {

    @Autowired
    private IDCardService dCardService;

    @Autowired
    private IUserService userService;


    public AjaxResult addCardValidation(CardAddRequest  request){
        if (StringUtils.isEmpty(request.getCardSn()) || request.getUserId() == null || StringUtils.isEmpty(request.getCardType())){
            return AjaxResult.error("必填项不能为空");
        }
        DCard select = new DCard();
        select.setParkId(request.getParkId());
        select.setCardSn(request.getCardSn());
        List<DCard> dCardList = dCardService.selectCardList(select);
        //卡号没有被添加过
        if(!CollectionUtils.isEmpty(dCardList)){
            User user = userService.selectUserById(dCardList.get(0).getUserId());
            if(user!=null){
            return AjaxResult.error("发卡失败！卡号【" + request.getCardSn() + "】已经被住户【" + user.getUserName() + "】绑定。");
            }
        }
        return AjaxResult.success();
    }

}
